<template>
  <div class="components-input-demo-presuffix">
    <!---->
    <a-input @click="openModal" placeholder="请点击选择营销订单" v-model="storeVals" readOnly :disabled="disabled">
      <a-icon slot="prefix" type="align-center" title="营销订单选择控件" />
      <a-icon v-if="storeVals" slot="suffix" type="close-circle" @click="handleEmpty" title="清空" />
    </a-input>
    <tsl-select-trade-order-modal
      ref="innerSelectModal"
      :modal-width="modalWidth"
      :multi="false"
      :status="status"
      :isCalcCost="isCalcCost"
      :isGeneratePackageProcementOrder="isGeneratePackageProcementOrder"
      @selectOk="handleOK"
    />
  </div>
</template>
<script>
import TslSelectTradeOrderModal from './modules/TslSelectTradeOrderModal'
import { underLinetoHump } from '@/components/_util/StringUtil'
export default {
  name: 'TslSelectTradeOrder',
  components: {
    TslSelectTradeOrderModal,
  },
  props: {
    modalWidth: {
      type: Number,
      default: 1000,
      required: false,
    },
    status: {
      type: Number,
      default: 999,
      required: false,
    },
    value: {
      type: String,
      required: false,
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
    // 自定义返回字段，默认返回 id
    customReturnField: {
      type: String,
      default: '',
    },
    // 存储字段 [key field]
    store: {
      type: String,
      default: 'code',
      required: false,
    },
    isCalcCost: {
      type: Number,
      default: undefined,
      required: false,
    },
    isGeneratePackageProcementOrder: {
      type: Number,
      default: undefined,
      required: false,
    },
  },
  data() {
    return {
      visible: false,
      confirmLoading: false,
      storeVals: '', //[key values]
    }
  },
  computed: {
    storeField() {
      let field = this.customReturnField
      if (!field) {
        field = this.store
      }
      return underLinetoHump(field)
    },
  },
  mounted() {
    this.storeVals = this.value
  },
  watch: {
    value(val) {
      this.storeVals = val
    },
  },
  methods: {
    openModal() {
      this.$refs.innerSelectModal.show()
    },
    handleOK(rows) {
      if (!rows && rows.length <= 0) {
        this.storeVals = ''
      } else {
        let arr1 = []
        for (let row of rows) {
          arr1.push(row[this.storeField])
        }
        this.storeVals = arr1.join(',')
      }
      this.$emit('change', this.storeVals, rows)
    },
    handleEmpty() {
      this.handleOK('')
    },
  },
  model: {
    prop: 'value',
    event: 'change',
  },
}
</script>

<style scoped>
.components-input-demo-presuffix .anticon-close-circle {
  cursor: pointer;
  color: #ccc;
  transition: color 0.3s;
  font-size: 12px;
}
.components-input-demo-presuffix .anticon-close-circle:hover {
  color: #f5222d;
}
.components-input-demo-presuffix .anticon-close-circle:active {
  color: #666;
}
</style>
